/**
 * @author 黄波
 * 创建日期：2011Aug 27, 2011
 * TODO// N/A
 */
package com.platform.manager.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.platform.bean.TreeBean;
import com.platform.manager.bo.ModuleBo;
import com.platform.pojo.Module;
import com.platform.pojo.UserInfo;

//根据session中的用户信息，获得当前用户的权限树信息
public class ModuleAction extends ManagerSupport {

	private boolean success;	//给前台EXTJS标识服务器返回是否正确
	private List<TreeBean> beans;
	
	/**
	 * @return the success
	 */
	public boolean isSuccess() {
		return success;
	}
	/**
	 * @param success the success to set
	 */
	public void setSuccess(boolean success) {
		this.success = success;
	}
	/**
	 * @return the beans
	 */
	public List<TreeBean> getBeans() {
		return beans;
	}
	/**
	 * @param beans the beans to set
	 */
	public void setBeans(List<TreeBean> beans) {
		this.beans = beans;
	}
	@Override
	public String execute() throws Exception {
		//取session中的用户id
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfo user = (UserInfo)session.get("user");
		if(user == null) {
			success = false;
			return SUCCESS;
		}
		ModuleBo bo = new ModuleBo();
		List<Module> modules = bo.getUserModules(user.getId());
		//将modules转化为beanlist
		for(Module module : modules) {
			TreeBean bean = new TreeBean();
			bean.setId(module.getId().toString());
			bean.setText(module.getModuleName());
			if(module.getSuperId() == 0 && bo.getChildModules(module.getId().toString()).size() > 0) {
				//父id为0且有子节点，则表示非叶子节点
				bean.setLeaf(false);
			} else {
				bean.setLeaf(true);
			}
			bean.setExpanded(false);
			if(beans == null) {
				beans = new ArrayList<TreeBean>();
			}
			beans.add(bean);
		}
		success = true;
		return SUCCESS;
	}
	
	
}
